package sh.wn36.vehiclemanage.xzh;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.util.Result;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@CrossOrigin
@Api("系统字典")
@RestController
@RequestMapping("/dictionary")
public class System_Dictionary_Controller {
    @Autowired
    private DictionaryService dictionaryService;




    @ApiOperation("获取大分类的列表")
    @GetMapping("/getDictionaryLargeList")
    public Result getList() {
        List<String> largeList = dictionaryService.getLargeClassificationList();
        return Result.OK().data("largeList", largeList).data("list",dictionaryService.list());
    }

    @ApiOperation("根据大分类列表获取子分类列表")
    @PostMapping("/getClassificationList")
    public Result getClassificationList(@ApiParam("前段的大分类的内容") @RequestBody(required = true) String largeClassification) {
        try {
            largeClassification = URLDecoder.decode(largeClassification, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        List<Dictionary> list = dictionaryService.list(null);
        if (largeClassification.endsWith("=")) {
            largeClassification = largeClassification.substring(0, largeClassification.length() - 1);
        }
        Dictionary tmp1 = dictionaryService.getByText(largeClassification);
        List<Dictionary> middleList= dictionaryService.getListByPidAndLev(tmp1.getId(),tmp1.getLev()+1);
        List<Dictionary> miniList=dictionaryService.getListByPidAndLev(tmp1.getId(),tmp1.getLev()+2);
        return Result.OK().data("middleList", middleList).data("miniList",miniList);
    }



    @ApiOperation(value = "获取字典列表(带分页) ")
    @PostMapping("/getListByPage/{currentPage}/{pageSize}")
    public Result getListByPage(@ApiParam(value = "当前页码") @PathVariable("currentPage") Integer currentPage,
                                @ApiParam(value = "每页大小") @PathVariable("pageSize") Integer pageSize,
                                @ApiParam(value = "查询内容") @RequestBody(required = false) DictionaryQuery systemDictionaryQuery) {
        System.out.println(currentPage+"currentPage");
        Page<DictionaryInfo> page = new Page<>(currentPage, pageSize);
        IPage<DictionaryInfo> dictionaryInfoIPage = null;
        if (systemDictionaryQuery != null) {
            String largeClassification = systemDictionaryQuery.getLargeClassification();
            String middleClassification = systemDictionaryQuery.getMiddleClassification();
            String miniClassification = systemDictionaryQuery.getMiniClassification();
            if (!StringUtils.isEmpty(miniClassification) ) {
                dictionaryInfoIPage = dictionaryService.getListByMini(page,miniClassification);
            } else if ((!StringUtils.isEmpty(middleClassification)) && (StringUtils.isEmpty(miniClassification))) {
                dictionaryInfoIPage = dictionaryService.getListByMiddle(page,middleClassification);
            } else if ((!StringUtils.isEmpty(largeClassification)) && (StringUtils.isEmpty(middleClassification))){
//                else ((!StringUtils.isEmpty(largeClassification)) && (StringUtils.isEmpty(middleClassification)))
                dictionaryInfoIPage = dictionaryService.getListByLarge(page,largeClassification);
            }else {
                dictionaryInfoIPage = dictionaryService.getList(page);
            }
        }else {
            dictionaryInfoIPage = dictionaryService.getList(page);
        }
        long total = page.getTotal();
        List<DictionaryInfo> dictionaryInfoList = dictionaryInfoIPage.getRecords();
        return Result.OK().data("total", total).data("list", dictionaryInfoList);
    }

    @ApiOperation("新增字典信息")
    @PostMapping("/save/{parentText}")
    public Result saveDictionary(@ApiParam("新增字典信息的内容")@RequestBody(required = true) Dictionary dictionary,@ApiParam("父类的文本")@PathVariable("parentText") String text) throws UnsupportedEncodingException {
        text = URLDecoder.decode(text, "UTF-8");
        System.out.println(text);
            if ("请选择上级".equals(text)){
            dictionary.setPid(0);
        }
        boolean b = dictionaryService.saveOrUpdate(dictionary);
        if (b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("根据id删除字典信息")
    @GetMapping("/remove/{id}")
    public Result removeById(@ApiParam("字典信息的id")@PathVariable("id")Integer id){
        boolean b = dictionaryService.removeById(id);
        if (b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("根据id获取字典信息")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam("需要查询的id")@PathVariable("id")Integer id){
        System.out.println("第一件出");
        return Result.OK().data("dictionary",dictionaryService.getById(id));
    }

    @ApiOperation("根据已选的字典信息删除相应的记录")
    @PostMapping("/removeAll")
    public Result removeAll(@ApiParam("需要删除记录的列表")@RequestBody(required = true) List<Object> list){
        List<Integer> idList= null;
        for (Object dictionary : list) {
            idList.add(((Dictionary)dictionary).getId());
        }
        boolean b = dictionaryService.removeByIds(idList);
        if (b) {
            return Result.OK();
        }else  {
            return Result.Fail();
        }
    }

    @ApiOperation("获取父类列表")
    @PostMapping("/getParent/{lev}")
    public Result getParent(@ApiParam("通过lev获取父类列表")@PathVariable("lev")Integer lev){
        List<DictionaryInfo> list = null;
        if (lev>1){
            list=dictionaryService.getListByLev(lev-1);
        }
        return  Result.OK().data("parentList",list);
    }
    @ApiOperation("通过子类Id查询父类列表")
    @GetMapping("/getParentById/{id}")
    public Result getListById(@ApiParam("子类id")@PathVariable("id")Integer id){
        List<Dictionary> list = null;
        Dictionary tmp = dictionaryService.getById(id);
        if (tmp.getLev()!=1){
            list= dictionaryService.getListBySonPid(tmp.getPid());
        }
        return Result.OK().data("parentList",list);
    }


}